/**
 * home.js
 * author jtr
 * 2017.7
 */
$(function() {
    /*------------------------topToZero-------------------------*/
    setTimeout(function(){
        $(document).scrollTop(0);
    },200);
    
    setTimeout(function() {
        $('.bg-swf').css('opacity', 1);
    }, 1000);
    /*------------------------class-------------------------*/
    /**
     * 克隆
     * @param {[type]} obj 克隆对象
     */
    function Clone(obj) {
        for (var key in obj) {
            this[key] = typeof obj[key] !== 'object' ? obj[key] : new Clone(obj[key]);
        }
    }
    /**
     * 显示+轮播
     * @param {[type]} btn     打开选项卡
     * @param {[type]} box     容器
     * @param {[type]} close   关闭选项卡
     * @param {[type]} flag    是否有父级容器
     * @param {[type]} btn2    选项卡左右按钮
     * @param {[type]} content 文本内容
     * @param {[type]} roll    滚动条
     * @param {[type]} bar     滚轮
     * @param {[type]} article 文本容器
     */
    function Play(btn, box, close, flag, btn2, content, roll, bar, article) {
        this.btn = btn;
        this.box = box;
        this.close = close;
        this.doc = $('body');
        this.flag = flag;
        this.index = null;
        this.btn2 = btn2;
        this.length = box.length;
        this.content = content;
        this.roll = roll;
        this.bar = bar;
        this.article = article;
    }
    Play.prototype = {
        exe: function() {
            this.show();
            this.hidden();
            this.change();
            this.init();
            this.scrMove();
            return this;
        },
        scrMove: function() {
            if (this.flag) {
                var This = this;
                var $doc = $(document);
                //设置滚轮高度;
                var ch = this.article.eq(0).outerHeight() * this.roll.eq(0).outerHeight();
                for (var i = 0; i < this.length; i++) {
                    var txt = this.content.get(i);
                    bh = ch / txt.sh;
                    this.bar.eq(i).height(bh);
                    this.bar.get(i).barMax = this.roll.eq(0).outerHeight() - bh;
                }
                //自定义滚动条
                for (var i = 0; i < this.length; i++) {
                    (function(i) {
                        var bar = This.bar.get(i);
                        var $bar = This.bar.eq(i);
                        var txt = This.content.get(i);
                        var $txt = This.content.eq(i);
                        var art = This.article[i];
                        var $roll = This.roll.eq(i);
                        //滚轮
                        This.mousewheel(art, function(e, d) {
                            var top = $bar.position().top;
                            d < 0 ? top += 10 : top -= 10;
                            top = Math.min(top, bar.barMax);
                            top = Math.max(top, 0);
                            $bar.css('top', top);
                            $txt.css('top', -top / txt.prop);
                        }, false);
                        //拖
                        $bar.mousedown(function(e) {
                            var dy = e.clientY;
                            var start = $bar.position().top;
                            $doc.mousemove(function(e) {
                                var y = e.clientY,
                                    top = y - dy + start;
                                top = Math.min(top, bar.barMax);
                                top = Math.max(top, 0);
                                $bar.css('top', top);
                                $txt.css('top', -top / txt.prop);

                            }).mouseup(function() {
                                $(this).off('mousemove').off('mouseup');
                            });
                            return false;
                        });
                        //点击
                        $roll.click(function(e) {
                            if (e.target === this) {
                                var top = $bar.position().top;
                                var y = e.clientY - ($(this).offset().top - $doc.scrollTop());
                                y > top ? top += 100 : top -= 100;
                                top = Math.min(top, bar.barMax);
                                top = Math.max(top, 0);
                                $bar.css('top', top);
                                $txt.css('top', -top / txt.prop);
                            }
                        });
                    })(i);
                }
            }
        },
        mousewheel: function(obj, eFn, bool) { //封装滚轮
            obj.onmousewheel = Fn;
            obj.addEventListener && obj.addEventListener('DOMMouseScroll', Fn);

            function Fn(e) {
                e = e || window.event;
                var d = -e.detail / 3 || e.wheelDelta / 120; //下滚-1/上滚1
                eFn.call(this, e, d);
                return bool;
            }
        },
        init: function() {
            if (this.flag) {
                this.box.parent().toggleClass('dis-n');
                this.box.removeClass('dis-n');
                this.box.addClass('dis-b');
                this.content.each(function() {
                    this.sh = this.scrollHeight;
                    this.prop = $(this).parent().outerHeight() / this.sh;
                });
                this.box.parent().toggleClass('dis-n');
                this.box.removeClass('dis-b');
                this.box.addClass('dis-n');

            }
        },
        show: function() {
            var This = this;
            this.btn.each(function() {
                this.index = $(this).index();
            });
            this.btn.click(function() {
                This.index = this.index;
                if (This.flag) {
                    This.box.eq(This.index).parent().toggleClass('dis-n');
                }
                This.box.eq(This.index).removeClass('dis-n');
                This.box.eq(This.index).addClass('dis-b');
                This.doc.toggleClass('noScroll');

                return false;
            });
            return this;
        },
        hidden: function() {
            var This = this;
            this.close.click(function() {
                if (This.flag) {
                    This.box.parent().toggleClass('dis-n');
                }
                This.box.removeClass('dis-b');
                This.box.addClass('dis-n');
                This.doc.toggleClass('noScroll');
                This.bar.css('top', 0);
                This.content.css('top', 0);
                return false;
            });
            return this;
        },
        change: function() {
            if (!this.flag) return;
            var This = this;
            var $docW = $(document).outerWidth();
            for (var i = 0; i < 2; i++) {
                (function(i) {
                    This.btn2.eq(i).click(function() {
                        This.box.addClass('dis-n');
                        This.box.removeClass('dis-b');
                        if (!i) {
                            This.index--;
                            This.index = This.index < 0 ? This.length - 1 : This.index;
                        } else {
                            This.index++;
                            This.index %= This.length;
                        }
                        This.box.eq(This.index).addClass('dis-b');
                        This.box.eq(This.index).removeClass('dis-n');
                        return false;
                    });
                })(i);
            }
            this.box.click(function(e) {
                if (e.target !== this) {
                    return false;
                }
                var x = e.clientX;
                var flag = x - $docW / 2;
                This.box.addClass('dis-n');
                This.box.removeClass('dis-b');
                if (flag <= 0) {
                    This.index--;
                    This.index = This.index < 0 ? This.length - 1 : This.index;
                } else {
                    This.index++;
                    This.index %= This.length;
                }
                This.box.eq(This.index).addClass('dis-b');
                This.box.eq(This.index).removeClass('dis-n');
                return false;
            });
        }
    };
    /**
     * 轮播图
     * @param {[type]} side  轮播选项
     * @param {[type]} btn   按钮
     * @param {[type]} index 当前图片
     */
    function Banner(side, btn, index) {
        this.side = side;
        this.btn = btn;
        this.index = index || 0;
        this.length = side.length;
    }
    Banner.prototype = {
        exe: function() {
            this.next();
            this.prev();
            this.current();
            this.init();
            return this;
        },
        init: function() {
            var prev, next;
            next = (this.index + 1) % this.length;
            prev = this.index - 1 < 0 ? this.length - 1 : this.index - 1;
            this.side.eq(prev).toggleClass('left');
            this.side.eq(this.index).toggleClass('mid');
            this.side.eq(next).toggleClass('right');
            this.side.eq(this.index).find('div').eq(0).toggleClass('box');
            this.side.eq(this.index).find('div').eq(1).toggleClass('box-right');
        },
        current: function() {
            var This = this;
            this.side.click(function() {
                var $index = $(this).index();
                This.change(function() {
                    This.index = $index == This.index ? This.index + 1 : $index;
                    This.index %= This.length;
                });
                return false;
            });
            return this;
        },
        next: function() {
            var This = this;
            var flag = true;
            this.btn.eq(1).click(function() {
                This.change(function() {
                    This.index++;
                    This.index %= This.length;
                });
                return false;
            });
            return this;
        },
        prev: function() {
            var This = this;
            var flag = false;
            this.btn.eq(0).click(function() {
                This.change(function() {
                    This.index--;
                    This.index = This.index < 0 ? This.length - 1 : This.index;
                });
            });
            return this;
        },
        change: function(fn) {
            var prev, next;
            next = (this.index + 1) % this.length;
            prev = this.index - 1 < 0 ? this.length - 1 : this.index - 1;
            this.side.eq(prev).removeClass('left');
            this.side.eq(this.index).removeClass('mid');
            this.side.eq(next).removeClass('right');
            this.side.eq(this.index).find('div').eq(0).removeClass('box');
            this.side.eq(this.index).find('div').eq(1).removeClass('box-right');
            fn();
            next = (this.index + 1) % this.length;
            prev = this.index - 1 < 0 ? this.length - 1 : this.index - 1;
            this.side.eq(prev).addClass('left');
            this.side.eq(this.index).addClass('mid');
            this.side.eq(next).addClass('right');
            this.side.eq(this.index).find('div').eq(0).addClass('box');
            this.side.eq(this.index).find('div').eq(1).addClass('box-right');
        }
    };
    /**
     * 懒加载
     * @param {[type]} title    标题
     * @param {[type]} infoList 子集
     */
    function LazyLoad(title, infoList) {
        this.title = title;
        this.infoList = infoList;
        this.flag = true;
        this.arr = [];
        this.win = $(window);
    }
    LazyLoad.prototype = {
        exe: function() {
            this.init(this.title, this.infoList);
            this.move();
        },
        init: function() {
            var This = this;
            for (var i = 0, length = arguments.length; i < length; i++) {
                arguments[i].each(function() {
                    this.oddTop = $(this).offset().top;
                    This.arr.push(this);
                });
            }
            this.title.addClass('lazy-hide');
            this.infoList.addClass('lazy-hide');
        },
        move: function() {
            if (this.flag) {
                var height = 0;
                var This = this;
                this.win.scroll(function() {
                    height = (This.win.scrollTop() + This.win.height()) * 1.1;
                    for (var i = This.arr.length - 1; i >= 0; i--) {
                        var obj = This.arr[i];
                        if (height >= obj.oddTop) {
                            //闭包!
                            (function() {
                                var $This = $(obj);
                                var time = $This.index() % 3 * 200;
                                setTimeout(function() {
                                    $This.removeClass('lazy-hide');
                                }, time);
                                This.arr.splice(i, 1);
                            })();
                        }
                    }
                    if (This.arr.length === 0) {
                        This.flag = false;
                        return false;
                    }
                });
            }
        }
    };
    /*------------------------项目初始化-------------------------*/
    (function(){
        var $yys = $('header > .yys'),
            $logo = $('header > .login'),
            $download = $('header > .download'),
            $videoLogo = $('header > figure.animate > .logo');
        $(function(){
            $yys.addClass('on');
            $logo.addClass('on');
            $download.addClass('on');
            $videoLogo.addClass('on');
        });
    })();
    /*------------------------模拟懒加载-------------------------*/
    (function() {
        var $newVer = $('#newVer'),
            $newH1 = $newVer.find('h1'),
            $newDd = $newVer.find('dd'),
            $feature = $('#feature'),
            $feaH1 = $feature.find('h1'),
            $feaNav = $('#f-banner');
        var lazy = new LazyLoad($feaH1, $feaNav).exe();
        var lazy2 = new LazyLoad($newH1, $newDd).exe();
    })();
    /*------------------------头部-------------------------*/

    (function() {
        var $videoBtn = $('#videos-btn').eq(0),
            $videoBox = $('#videos-box').eq(0),
            $videoClose = $('#videos-close').eq(0);
        var headerPlay = new Play($videoBtn, $videoBox, $videoClose).exe();
    }());
    /*------------------------特色游戏-------------------------*/
    (function() {
        var $nav = $('#f-banner'),
            $side = $nav.find('dd'),
            length = $side.length,
            $btn = $nav.find('.btn'),
            num = Math.floor(Math.random() * length);
        var fBanner = new Banner($side, $btn, num).exe();
    })();
    /*------------------------新版情报-------------------------*/
    (function() {
        var $content = $('#content'),
            $wrap = $content.find('.wrap'),
            $newDd = $('#newVer').find('dd'),
            $close = $content.find('.close'),
            $btn = $content.find('.btn');
        var $art = $content.find('article'),
            $txt = $content.find('.txt'),
            $scrBar = $content.find('.scroll-bar'),
            $bar = $content.find('.bar');
        //显示+轮播
        var newInfo = new Play($newDd, $wrap, $close, true, $btn, $txt, $scrBar, $bar, $art).exe();
    })();

});
